package com.taoyi.mhouse.binder;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.taoy.recyclebinder.DataBindAdapter;
import com.taoy.recyclebinder.DataBinder;
import com.taoyi.mhouse.R;
import com.taoyi.mhouse.util.UIUtils;
import com.tempo.widget.IndicatorView;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by tempo on 2015/11/13.
 */
public class BannerBinder extends DataBinder<BannerBinder.ViewHolder> {
    private ViewPager vpBanner;
    public BannerBinder(DataBindAdapter dataBindAdapter) {
        super(dataBindAdapter);
    }
    @Override
    public ViewHolder newViewHolder(ViewGroup parent) {
        View v= LayoutInflater.from(UIUtils.getContext()).inflate( R.layout.banner_layout, parent,false);
        vpBanner = (ViewPager) v.findViewById(R.id.vp_banner);
        MyPagerAdapter myPagerAdapter=new MyPagerAdapter();

        vpBanner.setAdapter(myPagerAdapter);
        IndicatorView indicatorView= (IndicatorView) v.findViewById(R.id.indicator);
        indicatorView.setViewPager(vpBanner,4);
        return new ViewHolder(v);
    }

    @Override
    public void bindViewHolder(ViewHolder holder, int position) {
    }
    @Override
    public int getItemCount() {
        return 1;
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewPager vpBanner;
        public ViewHolder(View itemView) {
            super(itemView);
            vpBanner= (ViewPager) itemView.findViewById(R.id.vp_banner);
            MyPagerAdapter myPagerAdapter=new MyPagerAdapter();
            vpBanner.setAdapter(myPagerAdapter);
            vpBanner.setCurrentItem(Integer.MAX_VALUE/2);
        }

    }
    private static class MyPagerAdapter extends PagerAdapter{
        private List<Integer> data;
        public MyPagerAdapter() {
            data = new ArrayList<>();
            data.add(R.drawable.a1);
            data.add(R.drawable.a2);
            data.add(R.drawable.a3);
            data.add(R.drawable.a4);
        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE-1;
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv=new ImageView(UIUtils.getContext());
            if(data!=null&&data.size()>0){
                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                iv.setImageResource(data.get(position%data.size()));
                container.addView(iv);
            }
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
